<?php
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\Wiki as mWiki;
use Biz\model\wiki\WikiBasic as mWikiBasic;
use Biz\model\wiki\WikiContent as mWikiContent;
use Biz\tools\RealUrl;

$url = "https://xeppopenapi.html5.qq.com/api/router?method=xepp.productlist.query.post";
$pageSize = 50;
$page = 1;


$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
//curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);

$list = [];

while (true) {
    $params = [
        "PageNo"   => $page,
        "PageSize" => $pageSize
    ];
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($httpCode == 200) {
        $rs = json_decode($response, true);
        $total = $rs['Total'];
        $product = $rs['Product'];
        $maxPage = ceil($total / $pageSize);
        $list = array_merge($list, $product);
        if ($page >= $maxPage) break;
        $page++;
        continue;
    } else {
        var_dump(compact('page', 'httpCode', 'response'));
    }
    break;
}
curl_close($ch);

var_dump(array_column($list, 'Title', 'SpuID'));

$channel = mWiki::CHANNEL_XIAOEPINPIN;
$channelType = mWiki::CHANNEL_TYPE_XIAOEPINPIN;
foreach ($list as $v) {
    $channel_spu_id = $v['SpuID'];
    $price = $v['Price'] / 100;
    $images = $v['ImgUrl'] ?? [];
    $DetailUrl = $v['DetailUrl'] ?? '';
    $AppUrl = $v['AppUrl'] ?? '';
    $data = [
        'title'                 => $v['Title'],
        'channel'               => $channel,
        'channel_jump_params'   => json_encode(compact('DetailUrl', 'AppUrl')),
        'channel_type'          => $channelType,
        'channel_brand'         => $v['Brand'] ?? '',
        'channel_shop_id'       => $v['ShopID'] ?? 0,
        'channel_shop_name'     => $v['ShopName'] ?? '',
        'channel_spu_id'        => $channel_spu_id,
        'channel_category_name' => $v['Category'] ?? '',
        'category_id'           => 0,
        'second_category_id'    => 0,
        'last_category_id'      => 0,
        'cover_img'             => $images[0] ?? '',
        'min_price'             => $price,
        'max_price'             => $price,
        'status'                => mWiki::STATUS_WAIT_CHECK,
    ];
    $wikiData = $data;
    $SpuDetailImg = $v['SpuDetailImg'] ?? [];
    $content = '';
    foreach ($SpuDetailImg as $img) {
        $content .= "<img src='$img'>";
    }
    $contentData = [
        'imgs'    => $images ? json_encode($images, true) : '',
        'content' => $content,
    ];

    $channelArr = in_array($channelType, [RealUrl::URL_TYPE_TAOBAO, RealUrl::URL_TYPE_TMALL]) ? [RealUrl::URL_TYPE_TAOBAO, RealUrl::URL_TYPE_TMALL] : $channelType;


    $wikiInfo = mWiki::getInfoByChannelSpu($channel, $channel_spu_id);
    if ($wikiInfo) {
        $wikiId = $wikiInfo['id'];

        mWiki::updateData($wikiData, ['id' => $wikiId]);
        $contentInfo = mWikiContent::getByWhere(['wiki_id' => $wikiId]);
        if ($contentInfo) {
            if ($wikiInfo['status'] != mWiki::STATUS_CHECKED) {
                if (!$contentData['imgs'] || $contentInfo['imgs']) {
                    unset($contentData['imgs']);
                }
                if (!$contentData['content'] || $contentInfo['content']) {
                    unset($contentData['content']);
                }
                if ($contentData) {
                    mWikiContent::updateData($contentData, ['wiki_id' => $wikiId]);
                }
            }
        } else {
            $contentData['wiki_id'] = $wikiId;
            mWikiContent::insertData($contentData);
        }
    } else {
        $wikiBasicData = [
            'brand_id'           => 0,
            'title'              => $wikiData['title'],
            'basic_state'        => mWikiBasic::STATE_NORMAL,
            'category_id'        => $wikiData['category_id'],
            'second_category_id' => $wikiData['second_category_id'],
            'last_category_id'   => $wikiData['last_category_id'],
        ];
        $basicId = mWikiBasic::insertData($wikiBasicData);
        $wikiData['basic_id'] = $basicId;
        $wikiData['source_basic_id'] = $basicId;
        $wikiData['is_default'] = 1;
        $wikiId = mWiki::insertData($wikiData);
        $contentData['wiki_id'] = $wikiId;
        mWikiContent::insertData($contentData);
    }
}

